package com.jhx.mall.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


/**
 * 用于解决中文乱码的过滤器
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-17 - 14:36
 */
@WebFilter(filterName = "EncoderFilter", urlPatterns = "/*")
public class EncoderFilter implements Filter {

    // 处理过滤
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        // 如果本次请求的字符编码是null
        if (req.getCharacterEncoding() == null) {
            // 设置本次请求的字符编码为UTF-8
            req.setCharacterEncoding("UTF-8");
        }
        if (resp.getContentType() == null) {
            // 设置响应数据的内容类型为application/json;
            resp.setContentType("application/json;charset=UTF-8");
        }
        chain.doFilter(req, resp); // 放行
    }
}
